/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\rgnpolygon.h"

//---------------------------------------------------------------------------

RgnPolygon::RgnPolygon(void)
{
	theType = __rptNone;
}

RgnPolygon::~RgnPolygon(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void RgnPolygon::CleanUp(void)
{
	MListElement *elem;
	RgnPoint *pt;

	elem = points.GetHead();
	while(elem!=NULL){
		pt = (RgnPoint*)elem->Data.Data;
		if(pt!=NULL){
			elem->Data.Data = NULL;
			delete pt;
		}
		elem = elem->Next;
	}
	points.Clear();
}

//---------------------------------------------------------------------------

RgnPloygonType RgnPolygon::GetType(void)
{
	return theType;
}

void RgnPolygon::SetType(RgnPloygonType typeIn)
{
	theType = typeIn;
}

//---------------------------------------------------------------------------

bool RgnPolygon::AddPoint(int aIn, int bIn, int cIn, int dIn)
{
	RgnPoint *pt;

	try{
		pt = new RgnPoint;
	}
	catch(...){
		pt = NULL;
	}
	if(pt==NULL) return false;

	if(!pt->Set(aIn, bIn, cIn, dIn)){
		delete pt;
		return false;
	}

	if(!points.AddLast(pt)){
		delete pt;
		return false;
	}

	return true;
}

//---------------------------------------------------------------------------

int RgnPolygon::GetCount(void)
{
	return points.GetCount();
}

//---------------------------------------------------------------------------

int RgnPolygon::GetX(int idxIn, int wIn)
{
	RgnPoint *pt;

	if(idxIn < 0) return 0;
	if(idxIn > points.GetCount()) return 0;

	pt = (RgnPoint*)points.GetItem(idxIn);
	if(pt==NULL) return 0;

	return pt->GetX(wIn);
}

int RgnPolygon::GetY(int idxIn, int hIn)
{
	RgnPoint *pt;

	if(idxIn < 0) return 0;
	if(idxIn > points.GetCount()) return 0;

	pt = (RgnPoint*)points.GetItem(idxIn);
	if(pt==NULL) return 0;

	return pt->GetY(hIn);
}

//---------------------------------------------------------------------------
